package cn.ycms.web.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import cn.ycms.utils.StringUtils;




public abstract class Action {

	public String redirect(HttpServletRequest req, ParamForm param) {
		req.setAttribute("info", param.getInfo());
		String url = param.getUrl();
		boolean b = true;
		if (url == null || StringUtils.isNull(url)) {
			b = false;
			url = "";
		}
		Map<String, String> m = param.getParam();
		for (String key : m.keySet()) {
			System.out.println(key);
			req.setAttribute(key, m.get(key));
			if (url.indexOf('?') == -1) {
				url = url.concat("?" + key + "=" + m.get(key));
			} else {
				url = url.concat("&" + key + "=" + m.get(key));
			}
		}
		if (b) {
			req.setAttribute("url", url);
		}
		req.setAttribute("ref", param.getRef());
		return "/org/info";
	}
	public String redirect2(HttpServletRequest req, ParamForm param) {
		req.setAttribute("info", param.getInfo());
		String url = param.getUrl();
		boolean b = true;
		if (url == null || StringUtils.isNull(url)) {
			b = false;
			url = "";
		}
		Map<String, String> m = param.getParam();
		for (String key : m.keySet()) {
			System.out.println(key);
			req.setAttribute(key, m.get(key));
			if (url.indexOf('?') == -1) {
				url = url.concat("?" + key + "=" + m.get(key));
			} else {
				url = url.concat("&" + key + "=" + m.get(key));
			}
		}
		if (b) {
			req.setAttribute("url", url);
		}
		req.setAttribute("ref", param.getRef());
		return "/org/info2";
	}
	public void setAttrubute(Map<String, Object> params, HttpServletRequest req) {
		if (params != null)
			for (String key : params.keySet()) {
				req.setAttribute(key, params.get(key));
			}
	}
	@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

}
